package com.citywithincity.db.utils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Arrays;

public class SimpleBlob implements Blob, Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3329101340879323956L;

	
	private byte[] bytes;
	
	public SimpleBlob(byte[] bytes){
		this.bytes = bytes;
	}
	
	@Override
	public void free() throws SQLException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public InputStream getBinaryStream() throws SQLException {
		
		return new ByteArrayInputStream(bytes);
	}

	@Override
	public InputStream getBinaryStream(long arg0, long arg1) throws SQLException {
		return new ByteArrayInputStream(bytes);
	}

	@Override
	public byte[] getBytes(long pos, int length) throws SQLException {
		
		return Arrays.copyOfRange(bytes, (int) pos, (int )(pos + length));
	}

	@Override
	public long length() throws SQLException {
		return bytes.length;
	}

	@Override
	public long position(byte[] arg0, long arg1) throws SQLException {
		throw new RuntimeException();
	}

	@Override
	public long position(Blob arg0, long arg1) throws SQLException {
		throw new RuntimeException();
	}

	@Override
	public OutputStream setBinaryStream(long arg0) throws SQLException {
		throw new RuntimeException();
	}

	@Override
	public int setBytes(long arg0, byte[] arg1) throws SQLException {
		throw new RuntimeException();
	}

	@Override
	public int setBytes(long arg0, byte[] arg1, int arg2, int arg3) throws SQLException {
		throw new RuntimeException();
	}

	@Override
	public void truncate(long arg0) throws SQLException {
		bytes = new byte[0];
	}

}
